/**
 * *************************************
 * Copyright(c)2016 Dingdang Yixia 
 * All Rights Reserved.
 *
 * @File: CustomerServiceImpl.java
 * @Package: com.ddyx.service.impl
 * @Description:
 * 
 * @Version: V1.0
 * @Author: Yidong.Xiang
 * @Date: 2017年8月24日 下午12:43:46
 * 
 * *************************************
 */
package com.ddyx.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.ddyx.common.util.ResponseJson;
import com.ddyx.entity.Customer;
import com.ddyx.entity.Deal;
import com.ddyx.entity.DealDetails;
import com.ddyx.entity.EventDealslog;
import com.ddyx.entity.EventLog;
import com.ddyx.mapper.CustomerMapper;
import com.ddyx.mapper.DealDetailsMapper;
import com.ddyx.mapper.DealMapper;
import com.ddyx.mapper.EventDealslogMapper;
import com.ddyx.mapper.EventLogMapper;
import com.ddyx.service.ICustomerService;

/**
 * @ClassName: CustomerServiceImpl.java
 * @Module: 顾客业务逻辑实现类模块
 * @Description: 
 * 
 * @author: Yidong.Xiang
 * @date: 2017年8月24日 下午12:43:46
 * 
 */
@Service("customerService")
public class CustomerServiceImpl implements ICustomerService {
	private static final Logger log = LoggerFactory.getLogger(CustomerServiceImpl.class);
	@Resource
	CustomerMapper customerMapper;
	@Resource
	DealMapper dealMapper;
	@Resource
	DealDetailsMapper dMapper;
	@Resource
	EventLogMapper eventLogMapper;
	@Resource
	EventDealslogMapper edMapper;
	
	@Override
	public String listVip(int page, int size, String filter) {
		int start = 0;
		if(size<1)
			size=20;//默认每页20条
		if(page<1)
			page = 1;//默认第1页
		start = (page-1)*size;
		int total = customerMapper.countVip(start, size, filter);
		if(total<1)
			return ResponseJson.getInstance().toString();
		List<Map<String,Object>> list = customerMapper.findVip(start, size, filter);
		return ResponseJson.getInstance()
				.setPage(page).setSize(size).setTotal(total).setData(list).toString();//返回有记录的JSON格式文本
	}

	@Override
	public String listGuest(int page, int size, String filter) {
		int start = 0;
		if(size<1)
			size=20;//默认每页20条
		if(page<1)
			page = 1;//默认第1页
		start = (page-1)*size;
		int total = customerMapper.countGuest(start, size, filter);
		if(total<1)
			return ResponseJson.getInstance().toString();
		
		List<Map<String,Object>> list = customerMapper.findGuest(start, size, filter);
		return ResponseJson.getInstance()
				.setPage(page).setSize(size).setTotal(total).setData(list).toString();//返回有记录的JSON格式文本
	}

	@Override
	public String getCustomer(String contactId) {
		List<Map<String,Object>> list = customerMapper.getCustomer(contactId);
		return ResponseJson.getInstance().setTotal(1).setData(list).toString();//返回有记录的JSON格式文本
	}

	@Override
	public String getDocument(int id) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public String listDeal(int page, int size, int customerId) {
		int start = 0;
		if(size<1)
			size=20;//默认每页20条
		if(page<1)
			page = 1;//默认第1页
		start = (page-1)*size;
		int total = dealMapper.countByCustomerId(customerId);
		
		List<Map<String,Object>> list = dealMapper.findByCustomerId(start, size, customerId);
		return ResponseJson.getInstance()
				.setPage(page).setSize(size).setTotal(total).setData(list).toString();//返回有记录的JSON格式文本
	}

	@Override
	public String listEvent(int page, int size, int faceId) {
		int start = 0;
		if(size<1)
			size=20;//默认每页20条
		if(page<1)
			page = 1;//默认第1页
		start = (page-1)*size;
		int total = eventLogMapper.countEventByFaceId(faceId);
		if(total<1)
			ResponseJson.getInstance().toString();
		
		List<EventLog> list = eventLogMapper.findEventByFaceId(start, size, faceId);
		return ResponseJson.getInstance()
				.setPage(page).setSize(size).setTotal(total).setData(list).toString();//返回有记录的JSON格式文本
	}
	
	@Override
	public int addCustomer(int faceId,String customerName, String nickname, String gender, String email, String mobile,
			String address,int isvip,String contactId) {
		Customer record = new Customer();
		record.setCustomerUuid(UUID.randomUUID().toString());
//		record.setFaceId(faceId);
		record.setCustomerName(customerName);
		record.setNickname(nickname);
		record.setGender(gender);
		record.setEmail(email);
		record.setMobile(mobile);
		record.setAddress(address);
		record.setIsvip(isvip);
		record.setContactId(contactId);
		return customerMapper.insert(record);
	}

//	@Override
//	public int editCustomer(int customerId, String customerName, String nickname, String gender, String email,
//			String mobile, String address,int isvip) {
//		Customer record = new Customer();
//		record.setCustomerName(customerName);
//		record.setNickname(nickname);
//		record.setGender(gender);
//		record.setEmail(email);
//		record.setMobile(mobile);
//		record.setAddress(address);
////		record.setIsvip(isVip);
//////		record.setFaceId(faceId);
////		record.setContactId(contactId);
////		record.setTags(tags);
//		return customerMapper.updateByCustomerId(record);
//	}
	@Override
	public int editCustomer(JSONObject customer) {

		String customerName = customer.getString("customerName");
		String nickname = customer.getString("nickname");
		String gender = customer.getString("gender");
		String email = customer.getString("email");
		String mobile = customer.getString("mobile");
		String address = customer.getString("address");
		int isVip = customer.getInteger("isVip");
		String contactId = customer.getString("contactId");
		String tags = customer.getString("tags");
		
		Customer record = new Customer();
		record.setContactId(contactId);
		record.setCustomerName(customerName);
		record.setNickname(nickname);
		record.setGender(gender);
		record.setEmail(email);
		record.setMobile(mobile);
		record.setAddress(address);
		record.setIsvip(isVip);
		record.setTags(tags);
		return customerMapper.updateByContactId(record);
	}
	@Transactional
	public Map<String,Object> setDeal(String billNo, int customerId, int storeId) {
		// TODO Auto-generated method stub
		Map<String, Object> map = new HashMap<>();
		EventLog eventLog = new EventLog();
		EventDealslog eventDealslog = new EventDealslog();
		try {	
			Deal deal = dealMapper.selectDealByMasterId(billNo, customerId, storeId);
			Customer customer = customerMapper.selectByPrimaryKey(customerId);
			eventLog.setContactId(customer.getContactId());
			eventLog.setEventType("2");
			eventLogMapper.insert(eventLog);
			eventDealslog.setEventId(eventLog.getEventId());
			eventDealslog.setStoreName(deal.getStoreName());
			eventDealslog.setBillDate(deal.getBillDate());
			eventDealslog.setBillNo(deal.getBillNo());
			eventDealslog.setTotalAmount(deal.getTotalAmount());
			eventDealslog.setTotalQty(deal.getTotalQty());
			edMapper.insert(eventDealslog);
			dealMapper.insert(deal);
			int i = deal.getDealId();
			List<DealDetails> list = dMapper.selectByDealId(i,billNo);
			for (DealDetails dealDetails : list) {
				dMapper.insert(dealDetails);
			}
			dealMapper.updateStatus(billNo);
			map.put("status", true);
			map.put("data", list);
			return map;
		}catch (Exception e) {
			TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
			map.put("status", false);
			map.put("data", new JSONArray());
			return map;
		}
	}
	@Override
	public int saveAndUpdateCustomer(JSONObject customer) {
		
//		int customerId = customer.getIntValue("customerId");
		String customerName = customer.getString("customerName");
		String nickname = customer.getString("nickname");
		String gender = customer.getString("gender");
		String email = customer.getString("email");
		String mobile = customer.getString("mobile");
		String address = customer.getString("address");
		int isVip = customer.getInteger("isVip");
//		int faceId = customer.getInteger("faceId");
		String contactId = customer.getString("contactId");
		String tags = customer.getString("tags");
		
		Customer record = new Customer();
		record.setCustomerName(customerName);
		record.setNickname(nickname);
		record.setGender(gender);
		record.setEmail(email);
		record.setMobile(mobile);
		record.setAddress(address);
		record.setIsvip(isVip);
//		record.setFaceId(faceId);
		record.setContactId(contactId);
		record.setTags(tags);
		int r = customerMapper.updateByContactId(record);
		if(r==0)
			r = customerMapper.insert(record);
		return r;
	}

	@Override
	public String findByCustName(int page, int size, String customerName) {
		int start = 0;
		if(size<1)
			size=20;//默认每页20条
		if(page<1)
			page = 1;//默认第1页
		start = (page-1)*size;
		int total = customerMapper.countVip(start, size, customerName);
		if(total<1)
			return ResponseJson.getInstance().toString();
		List<Map<String,Object>> list = customerMapper.findByCustName(start, size, customerName);
		return ResponseJson.getInstance()
				.setPage(page).setSize(size).setTotal(total).setData(list).toString();//返回有记录的JSON格式文本
	}

	@Override
	public boolean saveHeadImage(int customerId, String headImage) {
		
		return customerMapper.saveHeadImage(customerId, headImage);
	}
}
